/**
 * 十分彩的共用逻辑
 */
(function() {
    var currTabId = 1;
    var tabDraws = [
        // 1：两面
        fillLM,
        
        // 1-8球
        function() { fill_table(1); },
        function() { fill_table(2); },
        function() { fill_table(3); },
        function() { fill_table(4); },
        function() { fill_table(5); },
        function() { fill_table(6); },
        function() { fill_table(7); },
        function() { fill_table(8); },

        // 连码
        fill_lianma,
    ];

    function fillLM() {
        var types = ["大", "小", "单", "双", "尾大", "尾小"];
        var div = $(".zonghe");
        div.empty();
        for (var i = 0; i < types.length; i++) {
            var t = types[i];
            if (t.length == 1)
                add_bet_item('和' + t, '总和' + t, div);
            else
                add_bet_item('和' + t, '总' + t, div);
        }

        var arr = ['一', '二', '三', '四', '五', '六', '七', '八'];
        var types = ['大', '小', '单', '双', '尾大', '尾小', '合单', '合双', '龙', '虎'];
        var arr2 = ['大', '小', '单', '双', '尾大', '尾小', '合单', '合双', '龙', '虎'];
        var div = $(".eight");
        div.empty();
        for (var i in arr) {
            add_bet_th("第" + arr[i] + "球", div);
            var ul = $('<ul class="am-avg-sm-2 bet"></ul>');
            for (var j = 0; j < types.length; j++) {
                if (j > 7 && i > 3) {
                    continue;
                }
                add_bet_item(arr[i] + types[j], arr2[j], ul);
            }
            div.append(ul);
        }
    }

    function fill_table(index) {
        // 8球
        var arr = ['一', '二', '三', '四', '五', '六', '七', '八'];
        var types = ['大', '小', '单', '双', '合单', '合双', '尾大', '尾小'];
        {
            var i = index - 1;
            var div = $(".q" + (i + 1));
            div.empty();2

            // 1-18
            var ul = $('<ul class="am-avg-sm-3 bet"></ul>');
            for (var j = 1; j <= 18; j++) {
                var k = j < 10 ? "0" + j : j;
                var t = '<div class="bet-content round-1">' + j + '</div>';
                add_bet_item2(arr[i] + ":" + k, t, ul);
            }
            div.append(ul);

            var ul = $('<ul class="am-avg-sm-2 bet"></ul>');
            for (var j = 19; j <= 20; j++) {
                var k = j < 10 ? "0" + j : j;
                var t = '<div class="bet-content round-1">' + j + '</div>';
                add_bet_item2(arr[i] + ":" + k, t, ul);
            }

            for (var j in types) {
                var k = types[j];
                add_bet_item(arr[i] + k, k, ul);
            }

            if (i < 4) {
                add_bet_item(arr[i] + "龙", "龙", ul);
                add_bet_item(arr[i] + "虎", "虎", ul);
            }
            add_bet_item(arr[i] + "东", "东", ul);
            add_bet_item(arr[i] + "南", "南", ul);
            add_bet_item(arr[i] + "西", "西", ul);
            add_bet_item(arr[i] + "北", "北", ul);
            div.append(ul);

            var ul = $('<ul class="am-avg-sm-3 bet"></ul>');
            add_bet_item(arr[i] + "中", "中", ul);
            add_bet_item(arr[i] + "发", "发", ul);
            add_bet_item(arr[i] + "白", "白", ul);
            div.append(ul);
        }
    }

    function fill_lianma()
    {
        var ul = $(".page10 .n1-18"); ul.empty();
        for (var i = 1; i <= 18; i++) {
            ul.append('<li class="bet-item"><div class="bet-content round-1">' + i + '</div></li>');
        }
        var ul = $(".page10 .n19-20"); ul.empty();
        for (var i = 19; i <= 20; i++) {
            ul.append('<li class="bet-item"><div class="bet-content round-1">' + i + '</div></li>');
        }

        var _drawBanner = function() {
            var type = $(".page10 .types .active").attr("value");
            $(".page10 .peilv span").attr("key", type);
            $(".page10 li").removeClass("bet-active");

            // 设置赔率
            var rate = gameRule.map[type] ? gameRule.map[type].rate : "";
            $(".page10 .peilv span").html(rate);
        };
        $(".page10 .types li").click(function(e) {
            $(".page10 .types li").removeClass("active");
            $(e.currentTarget).addClass("active");
            _drawBanner();
        });
    }

    // 需要自己处理数字被点击
    var clickBetItemF = function(target) {
        var n = target.find("bet-content").html();
        if ($(".page10").css("display") == 'none') return;
        if (target.attr("class") && target.attr("class").indexOf("bet-active") != -1) return;

        var item = $(".page10 .types .active");
        var min = parseInt(item.attr("min")), max = parseInt(item.attr("max"));
        if ($(".page10 .bet-active").length >= max) {
            alert2("不允许超过" + max + "个选项");
            return true;
        }

        // 可以选中，走通用逻辑
        return;
    }

    // 取得注单列表
    var extraListF = function(data) {
        if ($(".page10").css("display") == 'none') return data;

        var item = $(".page10 .types .active");
        var min = parseInt(item.attr("min"));
        var arr = $(".page10 .bet-active .bet-content");
        if (arr.length < min) return data;

        var k = item.attr("value");
        var numbers = [];
        for (var i = 0; i < arr.length; i++) {
            numbers.push($(arr[i]).html());
        }

        var v = $(".footer input").val().trim();
        v = parseInt(v);
        data[k + ":" + numbers.join(",")] = v * 100;
        return data;
    }

    // 需要自己处理下注的逻辑
    var calcBetListF = function(data) {
        if ($(".page10").css("display") == 'none') return null;
        var total = 0, count = 0;
        for (var k in data) {
            if (k.startsWith('组2') || k.startsWith('任2')) {
                var arr = k.split(':');
                var numbers = arr[1].split(',');
                var i = numbers.length * (numbers.length - 1) / 2;
                count += i;
                total += data[k] * i;
                continue;
            }
            if (k.startsWith('组3') || k.startsWith('任3')) {
                var arr = k.split(':');
                var numbers = arr[1].split(',');
                var i = numbers.length * (numbers.length - 1) * (numbers.length - 2) / 6;
                count += i;
                total += data[k] * i;
                continue;
            }
            if (k.startsWith('任4')) {
                var arr = k.split(':');
                var numbers = arr[1].split(',');
                var i = numbers.length * (numbers.length - 1) * (numbers.length - 2) * (numbers.length - 3) / 24;
                count += i;
                total += data[k] * i;
                continue;
            }
            if (k.startsWith('任5')) {
                count++;
                total += data[k];
                continue;
            }
            count++;
            total += data[k];
        }
        return [count, total/100];
    }

    var unpackF = function(data) {
        if ($(".page10").css("display") == 'none') return data;

        var r = {};
        for (var k in data) {
            if (k.startsWith('组2')) {
                var arr = k.split(':');
                var numbers = arr[1].split(',');
                var list = group2(numbers);
                for (var i in list) {
                    r['组2:' + list[i].join(',')] = data[k];
                }
                r.group = '选二连组 ' + arr[1] + '[' + list.length + '组]_' + get_server_time();
            }
            else if (k.startsWith('组3')) {
                var arr = k.split(':');
                var numbers = arr[1].split(',');
                var list = group3(numbers);
                for (var i in list) {
                    r['组3:' + list[i].join(',')] = data[k];
                }
                r.group = '选三前组 ' + arr[1] + '[' + list.length + '组]_' + get_server_time();
            }
            else if (k.startsWith('任2')) {
                var arr = k.split(':');
                var numbers = arr[1].split(',');
                var list = group2(numbers);
                for (var i in list) {
                    r['任2:' + list[i].join(',')] = data[k];
                }
                r.group = '任选二 ' + arr[1] + '[' + list.length + '组]_' + get_server_time();
            }
            else if (k.startsWith('任3')) {
                var arr = k.split(':');
                var numbers = arr[1].split(',');
                var list = group3(numbers);
                for (var i in list) {
                    r['任3:' + list[i].join(',')] = data[k];
                }
                r.group = '任选三 ' + arr[1] + '[' + list.length + '组]_' + get_server_time();
            }
            else if (k.startsWith('任4')) {
                var arr = k.split(':');
                var numbers = arr[1].split(',');
                var list = group4(numbers);
                for (var i in list) {
                    r['任4:' + list[i].join(',')] = data[k];
                }
                r.group = '任选四 ' + arr[1] + '[' + list.length + '组]_' + get_server_time();
            }
            else
                r[k] = data[k];
        }
        return r;
    }

    var drawF2 = function() {
        var c = Result.last();
        if (!c || !c.red) return [];
        var red = c.red;
        var r = [];

        // 冠亚军和
        var he = gameRule.he(red);
        r.push(he);

        if (gameRule.isHeBig(red)) r.push("大");
        else if (gameRule.isHeSmall(red)) r.push("小");
        else r.push("和");

        if (gameRule.isOdd(he)) r.push("单");
        else r.push("双");

        if (gameRule.isWeiDa(he)) r.push("尾大");
        else r.push("尾小");
        return r;
    }

    var drawF = function(i) {
        i = parseInt(i);
        return '<span class="round">' + i + '</span>';
    }

    // 滚动
    var switchTabF = function() {
        new IScroll(".page10 .banner", {
            scrollX: true,
            scrollY: false,
            click: true,
        });
    };

    var initF = function(game, first) {
        window.game = game;
        initGame(game, function() {
            if (first !== false) fill_tabs(['两面', '第一球', '第二球', '第三球', '第四球', '第五球', '第六球', '第七球', '第八球', '连码']);
            currTabId = parseInt($(".menu-left a.active").attr("value"));
            $(".page" + currTabId).attr("flag", 1);
            var f = tabDraws[currTabId - 1];
            f();
        }, first);
    }

    var data = {
        drawResult: drawF,
        drawResult2: drawF2,
        init: initF,
        switchTab: switchTabF,
        clickBetItem: clickBetItemF,
        extraList: extraListF,
        calcBetList: calcBetListF,
        unpack: unpackF,
    }
    context.global.game["cqkl10"] = data;
    context.global.game["gdkl10"] = data;
    context.global.game["zykl10"] = data;
})();